import {
	defineStore
} from 'pinia'
import {
	ref
} from 'vue'
import {
	reactive
} from 'vue';

export default {
	state: reactive({
		userInfo: null
	})
	// ... 其他状态和方法 ...
};
export const userStore = defineStore("user", () => {
	let userInfo = ref({})
	let isLogin = ref(false)
	let token = ref("")
	let currentPos = ref({
		longitude: 0,
		latitude: 0
	})

	// 登录后保存登录信息
	function addUserInfo(id, username, phone, stoken) {

		userInfo.value = {
			id,
			username,
			phone
		}
		token.value = stoken
		isLogin.value = true
	}
	console.log(userInfo.value)
	console.log(token.value)

	// 清除用户登录状态
	function clearUserInfo() {

		userInfo.value = {

		}
		token.value = ""
		isLogin.value = false
	}

	function loadUserInfo() {

		let username = uni.getStorageSync("username")
		let userid = uni.getStorageSync("userid")
		let phone = uni.getStorageSync("phone")
		console.log(username, userid, phone)
		if (username && userid && phone) {
			userInfo.value.username = username
			userInfo.value.id = userid
			userInfo.value.phone = phone
			isLogin.value = true
		}

	}


	let Pos = ref(false)
	// 经纬度
	function setPos(longitude, latitude) {
		currentPos.value.longitude = longitude
		currentPos.value.latitude = latitude
		console.log(currentPos.value)
	}

	return {
		userInfo,
		isLogin,
		addUserInfo,
		clearUserInfo,
		setPos,
		token,
		currentPos
	}
})